/******************************************************************************
 * Copyright (C) 2012  IFDserver.org
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 
 * initial version by Jakob Beetz j.beetz@tue.nl
 * Eindhoven University of Technology
 *****************************************************************************/ 
package nl.cbnl.ifd;

import java.io.PrintWriter;
import java.io.StringReader;

import jsdai.dictionary.AEntity_declaration;
import jsdai.dictionary.ESchema_definition;
import jsdai.lang.ASchemaInstance;
import jsdai.lang.ASdaiModel;
import jsdai.lang.A_string;
import jsdai.lang.SchemaInstance;
import jsdai.lang.SdaiException;
import jsdai.lang.SdaiIterator;
import jsdai.lang.SdaiModel;
import jsdai.lang.SdaiRepository;
import jsdai.lang.SdaiSession;
import jsdai.lang.SdaiTransaction;

public class STEPLoader {
	private static SdaiSession session;
	private static SdaiTransaction transaction;
	private static SdaiRepository repo;
	private static SdaiModel model;
	ASdaiModel models;
	
	public  STEPLoader (String filename){
		    try{
			    SdaiSession.setLogWriter(new PrintWriter(System.out, true));
				session = SdaiSession.openSession();
				SdaiTransaction transaction = session.startTransactionReadWriteAccess();
	
				repo = session.createRepository("", null);	
				
				repo.openRepository();
				//repo.checkCreateSchemaInstance(jsdai.SIso_12006_3_version_16_draft_proposal.SIso_12006_3_version_16_draft_proposal.class);
				
				SchemaInstance schema = repo.createSchemaInstance("ifd", jsdai.SIso_12006_3_version_16_draft_proposal.SIso_12006_3_version_16_draft_proposal.class);
				ESchema_definition nschema = schema.getNativeSchema();
				
				model = repo.createSdaiModel("Model1", jsdai.SIso_12006_3_version_16_draft_proposal.SIso_12006_3_version_16_draft_proposal.class);
				
				repo = session.importClearTextEncoding(null,filename, repo);
				/*SchemaInstance si = session.getDataDictionary();
				System.out.println(si.getName());
				ASdaiModel associatedModels = si.getAssociatedModels();
				SdaiIterator iter = associatedModels.createIterator();
				while (iter.next()) {
					SdaiModel model = associatedModels.getCurrentMember(iter);
					ESchema_definition schemaDef = model.getDefinedSchema();	
					String s = schemaDef.getName(null);
				}
				*/
				
				
				models = repo.getModels(); 
				AEntity_declaration entity_declarations = nschema.getEntity_declarations(nschema,models);
				
					
				SdaiIterator it_models = models.createIterator(); 
				while (it_models.next()) {
				 model = models.getCurrentMember(it_models); 
				 ASchemaInstance schemas = model.getAssociatedWith();
				 SdaiIterator it_schemas = schemas.createIterator(); 
				 while (it_schemas.next()) {
					 SchemaInstance schemaI = schemas.getCurrentMember(it_schemas);
					 System.out.println("test");
					 
				 }
				System.out.println("Model <" + model.getName() + "> found"); 
				} 
				
	} catch (SdaiException e) {
		
		e.printStackTrace();
	}
	
		  
		    
	}
	
	public ASdaiModel getModels()
	{
		return models;
	}
	public SdaiModel getModel()
	{
		return model;
	}
	
	public void closeReader()
	{
		try {
			session.closeSession();
		} catch (SdaiException e) {
			
			e.printStackTrace();
		}
	}
	
	
}
